package com.jialiang.base.admin.account.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jialiang.auth.api.constants.PageData;
import com.jialiang.base.admin.account.domain.pojo.vo.PermissionListItemVO;
import com.jialiang.base.admin.account.domain.service.IPermissionService;
import com.jialiang.common.model.JsonObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 处理权限相关请求的控制器
 *
 * @author jialiang
 * @version 3.0
 */
@Slf4j
@RestController
@RequestMapping("/permissions")
@Validated
@Api(tags = "3. 权限管理")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    public PermissionController() {
        log.debug("创建控制器类对象：RoleController");
    }

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('/account/user/query')")
    @ApiOperation("查询权限列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "queryType", value = "查询类型，当需要查询全部数据时，此参数值应该是all")
    })
    public JsonObject<?> permissionList(Integer page, String queryType) {
        log.debug("开始处理【查询权限列表】的请求，页码：{}", page);
        if (page == null) {
            page = 1;
        }
        Integer pageNum = page > 0 ? page : 1;
        PageData<PermissionListItemVO> pageData;
        if ("all".equals(queryType)) {
            pageData = permissionService.list(1, Integer.MAX_VALUE);
        } else {
            pageData = permissionService.list(pageNum);
        }
        return JsonObject.success(pageData);
    }



}
